set more off


local control income attend male married familysize age hs coll grad white black

eststo clear

local j = 1

foreach dataset in sccbs gss ssi {

	use `dataset', clear

	rename family_size familysize
	
	if "`dataset'" == "sccbs" {
		keep if moderate == 0
		gen republican = conservative
	}
	if "`dataset'" != "sccbs" {
		keep if independent == 0
	}
	
	gen B = ""
	gen SE = ""
	gen P = ""
	gen var = ""
	
	local i = 1
	foreach x in `control' {
		su `x'
		replace `x' = (`x' - r(mean)) / (r(sd))
		reg `x' republican, robust
		replace B = string(_b[republican], "%20.02fc") if _n == `i'
		replace SE = string(_se[republican], "%20.02fc") if _n == `i'
		replace P = string(2*(1-normal(abs(_b[republican]/_se[republican]))), "%20.02fc") if _n == `i'
		replace var = "`x'" if _n == `i'
		local i = `i' + 1
	}
	
	keep if !mi(B)
	
	keep var B SE P
	gen dataset = "`dataset'"
	
	if `j' > 1 append using tmp
	save tmp, replace
	local j = `j' + 1
}

replace SE = "(" + SE + ")"
replace B = B + "*" if real(P) < .05
replace B = B + "*" if real(P) < .01
replace B = B + "*" if real(P) < .001

reshape i var
reshape j dataset, string
reshape xij B SE P
reshape xi
reshape wide

order var Bsccbs SEsccbs Psccbs

gen index = .							
replace index = 	1	if var == 	"income"		
replace index = 	2	if var == 	"attend"		
replace index = 	3	if var == 	"male"		
replace index = 	4	if var == 	"married"		
replace index = 	5	if var == 	"familysize"		
replace index = 	6	if var == 	"age"		
replace index = 	7	if var == 	"hs"		
replace index = 	8	if var == 	"coll"		
replace index = 	9	if var == 	"grad"		
replace index = 	10	if var == 	"white"		
replace index = 	11	if var == 	"black"		
replace var = 	"Income"	if var == 	"income"
replace var = 	"Religious Attendance"	if var == 	"attend"
replace var = 	"Male"	if var == 	"male"
replace var = 	"Married"	if var == 	"married"
replace var = 	"Family Size"	if var == 	"familysize"
replace var = 	"Age"	if var == 	"age"
replace var = 	"High School"	if var == 	"hs"
replace var = 	"College"	if var == 	"coll"
replace var = 	"Graduate"	if var == 	"grad"
replace var = 	"White"	if var == 	"white"
replace var = 	"Black"	if var == 	"black"

sort index
drop index
drop P*

#delimit;

listtex 
	using "tableA1.tex"
	,
	replace
	type rstyle(tabular)
	head(
		"\begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{9}{l}}"
		"\toprule&SCCBS&&GSS&&SSI""\\
		
		
		\midrule"
	)
	foot("\bottomrule\end{tabular*}")
	;
	
#delimit cr

